package com.changgou.util;


import com.changgou.file.FastDFSFile;
import org.csource.common.MyException;
import org.csource.common.NameValuePair;
import org.csource.fastdfs.ClientGlobal;
import org.csource.fastdfs.StorageClient;
import org.csource.fastdfs.TrackerClient;
import org.csource.fastdfs.TrackerServer;
import org.springframework.core.io.ClassPathResource;

import java.io.IOException;
import java.net.URL;

/**
 * 文件上传下载工具类
 */

public class FastDFSCilent {


    //加载配置文件
    static {


        //   String conf_filename =  new ClassPathResource("fdfs_client.conf").getPath();

        //初始化配置文件
        try {
            ClientGlobal.init("fdfs_client.conf");

        } catch (IOException e) {
            e.printStackTrace();
        } catch (MyException e) {
            e.printStackTrace();
        }

    }

    /**
     * 获取文件服务器地址
     *
     * @return 服务器地址
     */
    public static String getTrackerUrl() {

        try {
            //跟踪服务器的服务端
            TrackerServer trackerServer = getTrackerServer();
            //跟踪服务器地址
            String hostAddress = trackerServer.getInetSocketAddress().getAddress().getHostAddress();
            //端口
            int port = ClientGlobal.getG_tracker_http_port();
            String url = "http://" + hostAddress + ":" + port;
            return url;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 获取trackerServer 客户端
     *
     * @return
     * @throws IOException
     */
    private static TrackerServer getTrackerServer() throws IOException {

        // 1、创建跟踪服务器客户端
        TrackerClient trackerClient = new TrackerClient();

        // 2、获取跟踪服务器
        return trackerClient.getConnection();

    }


    /**
     * 上传文件
     *
     * @param fastDFSFile 文件对象
     * @return
     */
    public static String[] uploadFile(FastDFSFile fastDFSFile) {

        //赋值文件信息
        byte[] file_buff = fastDFSFile.getContent();//文件内容 字节数组
        String file_ext_name = fastDFSFile.getExt(); //文件拓展名
        NameValuePair[] meta_list = new NameValuePair[2];//存放文件的其他信息
        meta_list[0] = new NameValuePair(fastDFSFile.getAuthor());
        meta_list[1] = new NameValuePair(fastDFSFile.getName());

        // 1、创建跟踪服务器客户端
        TrackerClient trackerClient = new TrackerClient();
        try {
            // 2、获取跟踪服务器
            TrackerServer trackerServer = trackerClient.getConnection();
            // 3、创建存储服务器客户端
            StorageClient storageClient = new StorageClient(trackerServer, null);

            // 4、文件上传
            String[] uploadResult = storageClient.upload_file(file_buff, file_ext_name, meta_list);

            return uploadResult;

        } catch (IOException e) {
            e.printStackTrace();
        } catch (MyException e) {
            e.printStackTrace();
        }
        return null;

    }


    /**
     * 下载附件
     *
     * @param group_name      组名
     * @param remote_filename 文件名
     * @return
     */
    public static byte[] downFile(String group_name, String remote_filename) {

        try {
            TrackerServer trackerServer = getTrackerServer();
            // 3、创建存储服务器客户端
            StorageClient storageClient = new StorageClient(trackerServer, null);
            byte[] bytes = storageClient.download_file(group_name, remote_filename);
            return bytes;
        } catch (IOException e) {
            e.printStackTrace();
        } catch (MyException e) {
            e.printStackTrace();
        }

        return null;
    }

    /**
     * 下载附件
     * @param group_name 组名
     * @param remote_filename   附件名字
     */
    public static void deleteFile(String group_name, String remote_filename){
        try {
            TrackerServer trackerServer = getTrackerServer();
            // 3、创建存储服务器客户端
            StorageClient storageClient = new StorageClient(trackerServer, null);
            // 4、文件删除
            storageClient.delete_file(group_name, remote_filename);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
